package servlet.environment;




import commons.baseServlet.BaseServlet;
import commons.r.R;
import org.json.JSONObject;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * servlet.environment
 * User: 李雨行
 * Date: 2024/12/20 19:52
 * motto:   沉淀哥
 * Description:
 * Version: V1.0
 */
@WebServlet(value = "/environment/*")
public class Weather extends BaseServlet {
    public R api(HttpServletRequest request){
        String city=request.getParameter("area");
        WeatherService weatherService=new TianAPIService();
        String data=  weatherService.getAirQuality(city);
        JSONObject jsonObject1 = new JSONObject(data);
        JSONObject result = jsonObject1.getJSONObject("result");
//        System.out.println(result);
        String json= String.valueOf(result);
        JSONObject jsonObject = new JSONObject(json);

        // 创建一个 List 来存储 Map
        List<Map<String, Object>> list = new ArrayList<>();

        // 遍历 JSONObject 并将每个键值对放入 Map
        for (String key : jsonObject.keySet()) {
            Map<String, Object> map = new HashMap<>();
            map.put("key", key);  // 存储键
            map.put("value", jsonObject.get(key));  // 存储值
            list.add(map);  // 将 Map 放入 List 中
        }
        Map<String, Object> results = new HashMap<>();
        for (Map<String, Object> entry : list) {
            results.put((String) entry.get("key"), entry.get("value"));
        }
        System.out.println(results);
        List<Map<String, Object>> list1=new ArrayList<>();
        list1.add(results);
        return R.ok(list1);
    }

}
